package com.isunland.managesystem.adapter;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerRelationDepPerson.DepartmentPerson;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/5 15:01
 */

public class CustomerPersonAdapter extends BaseButterKnifeAdapter<DepartmentPerson> {

  private boolean showCustomerName;
  private boolean showCallAndMessageMenu;

  @SuppressWarnings("unused")
  public CustomerPersonAdapter(BaseVolleyActivity context, ArrayList<DepartmentPerson> objects) {
    super(context, objects);
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_new_list;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override
  protected void initView(DepartmentPerson item, BaseViewHolder baseHolder, int position) {
    ViewHolder holder = (ViewHolder) baseHolder;
    holder.tvTitle.setText(item.getStaffName());
    //holder.tvContent1.setVisibility(
    //    MyStringUtil.isEmpty(item.getStaffGender()) ? View.GONE : View.VISIBLE);
    //holder.tvContent2.setVisibility(
    //    MyStringUtil.isEmpty(item.getContactNum()) ? View.GONE : View.VISIBLE);
    //holder.tvContent3.setVisibility(
    //    MyStringUtil.isEmpty(item.getWeChatCode()) ? View.GONE : View.VISIBLE);
    holder.tvContent1.setText(MyStringUtil.connect(context.getString(R.string.departmentPersonSex),
        item.getStaffGender()));
    holder.tvContent2.setText(
        MyStringUtil.connect(context.getString(R.string.directordial), item.getContactNum()));
    holder.tvContent3.setText(
        MyStringUtil.connect(context.getString(R.string.weChatNumber), item.getWeChatCode()));
    holder.tvBottom.setText(item.getBlongCustomerName());
    holder.ivMore.setTag(item);
    String logoCotent = item.getStaffName();
    if (logoCotent == null) logoCotent = "";
    if (logoCotent.length() > 1) logoCotent = MyStringUtil.substring(logoCotent, 0, 1);

    //noinspection deprecation
    holder.ivLogo.setImageDrawable(TextDrawable.builder()
        .buildRound(logoCotent, context.getResources().getColor(R.color.primary_pink)));
  }

  public void setShowCustomerName(boolean showCustomerName) {
    this.showCustomerName = showCustomerName;
  }

  public void setShowCallAndMessageMenu(boolean showCallAndMessageMenu) {
    this.showCallAndMessageMenu = showCallAndMessageMenu;
  }

  public void initPopMenu(View view, final DepartmentPerson itemObj) {
    if (view == null || itemObj == null || MyStringUtil.isEmpty(itemObj.getContactNum())) {
      ToastUtil.show(R.string.numberException);
      return;
    }
    final String mPhone = itemObj.getContactNum();
    PopupMenu popupMenu = new PopupMenu(context, view);
    popupMenu.inflate(R.menu.menu_call_message);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.menu_item_call:
            try {
              Intent dialPhoneIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mPhone));
              context.startActivity(dialPhoneIntent);
            } catch (Exception e) {
              e.printStackTrace();
            }
            return true;
          case R.id.menu_item_message:
            try {
              Intent sendMessageIntent =
                  new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + mPhone));
              context.startActivity(sendMessageIntent);
            } catch (Exception e) {
              e.printStackTrace();
            }
            return true;
        }
        return false;
      }
    });
    popupMenu.show();
  }

  protected class ViewHolder extends BaseViewHolder {
    @BindView(R.id.iv_logo) ImageView ivLogo;
    @BindView(R.id.tv_title) TextView tvTitle;
    @BindView(R.id.tv_content1) TextView tvContent1;
    @BindView(R.id.tv_content2) TextView tvContent2;
    @BindView(R.id.tv_content3) TextView tvContent3;
    @BindView(R.id.tv_bottom) TextView tvBottom;
    @BindView(R.id.iv_more) ImageView ivMore;
    @BindView(R.id.tv_index) TextView tvIndex;
    @BindView(R.id.rl_right) RelativeLayout rlRight;

    ViewHolder(View view) {
      super(view);
      //tvContent3.setVisibility(View.GONE);
      if (!showCallAndMessageMenu) rlRight.setVisibility(View.GONE);
      if (!showCallAndMessageMenu) ivMore.setVisibility(View.GONE);
      if (!showCustomerName) tvBottom.setVisibility(View.GONE);
      tvIndex.setVisibility(View.GONE);
      ivMore.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          if (!(v.getTag() instanceof DepartmentPerson)) return;
          DepartmentPerson itemObj = (DepartmentPerson) v.getTag();
          initPopMenu(v, itemObj);
        }
      });
    }
  }
}
